package com.simon.resourcestatistics.service;

import java.util.Calendar;
import java.util.Date;

import javax.annotation.Resource;

import com.simon.resourcestatistics.dao.IConsumeLogDao;
import com.simon.resourcestatistics.dao.IMemberDao;
import com.simon.resourcestatistics.model.ConsumeLog;
import com.simon.resourcestatistics.model.Member;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class ConsumeLogServiceImpl implements IConsumeLogService {

    @Resource
    private IMemberDao memberDao;

    @Resource
    private IConsumeLogDao consumeLogDao;

    @Transactional
    @Override
    public ConsumeLog swipingCard(Integer readRoomId, String cardNo, boolean inOrOut) {
          // 根据卡号获得会员对象
          Member m=memberDao.getCardStatus(cardNo);
          ConsumeLog log=null;
          if(inOrOut){//如果是刷卡进入
            if(m.getStatus()==0){//状态正常
                //记录刷卡信息
                log=new ConsumeLog();
                log.setCardNo(cardNo);
                log.setReadRoomId(readRoomId);
                //设置离开时间为进入时间+30分钟
                Calendar nowTime=Calendar.getInstance();
                nowTime.add(Calendar.MINUTE,30);
                log.setOutTime(nowTime.getTime());
                log=consumeLogDao.save(log);
                //修改会员卡状态为资源室id
                m.setStatus(readRoomId);
                memberDao.save(m);
            }else{
                //状态异常,联系管理员
                System.out.println("会员卡状态异常，请联系管理员");
            }
          }else{//如果是刷卡离开
            //查询会员最后一次进入资源室的日志编号
            log = consumeLogDao.getMaxInTime(cardNo);
            //更新离开时间
            consumeLogDao.updateOutTime(log.getId(),new Date());
            //修改会员卡状态为0
            m.setStatus(0);

          }
          return log;//返回ConsumeLog对象
    }

}